package com.example.matchandroiddevices.scanlable;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.matchandroiddevices.R;

import java.util.ArrayList;
import java.util.HashMap;

public class ScanView extends Activity implements OnClickListener,OnItemSelectedListener{
	
	private TextView bGet;

	private EditText tvVersion;

	private TextView bSetAntenna;
	
	private Handler mHandler;
	

	
	private static final String TAG = "SacnView";
	
	private static final int MSG_SHOW_PROPERTIES = 0;
	private static final int MSG_UPDATE_Toast = 1;
	private static final int MSG_SHOW_SWR = 2;
	private static final int MSG_SHOW_POWER = 3;
    private byte CurrentAnt=0;
    byte[]VersionInfo=new byte[2];
    byte[]RFU =new byte[2];
    byte[]ReaderType=new byte[1];
    byte[]TrType=new byte[1];
    byte[]InventoryScanTime=new byte[1];

    CheckBox Ant1;
    CheckBox Ant2;
    CheckBox Ant3;
    CheckBox Ant4;
   
    //Check
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub  properties
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.scan_view);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		initView();
		
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case MSG_SHOW_PROPERTIES:
					showResult((String)msg.obj);
					break;
				case MSG_UPDATE_Toast:
					Toast.makeText(getApplicationContext(),(String)msg.obj,Toast.LENGTH_SHORT).show();
					break;
				
				default:
					break;
				}
			}
		};  
	}
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		//CurrentAnt = (byte)position;
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		uhf.close_reader();
	}
	
	private void initView(){
		
		
		Ant1 = (CheckBox)findViewById(R.id.check_ant1);
		Ant2 = (CheckBox)findViewById(R.id.check_ant2);
		Ant3 = (CheckBox)findViewById(R.id.check_ant3);
		Ant4 = (CheckBox)findViewById(R.id.check_ant4);
		
		
		tvVersion = (EditText)findViewById(R.id.version_text);
		
		bGet = (TextView)findViewById(R.id.getversion);	
		bGet.setOnClickListener(this);	
		
	
		
		bSetAntenna = (TextView)findViewById(R.id.setIvtAntenna);	
		bSetAntenna.setOnClickListener(this);

	}
	private void UpdateErrorToast(String errormsg)
	{
		if((errormsg==null)||(errormsg.length()==0))return;
		Message message = mHandler.obtainMessage();
		message.what = MSG_UPDATE_Toast;
		message.obj = errormsg;
		mHandler.sendMessage(message);
	}
	
	@SuppressLint("ResourceAsColor")
	@Override
	public void onClick(View view) {
		if(view ==bGet)
		{
			new Thread()
	        {
	            public void run() 
	            {
	            	int result=HfData.reader.GetReaderInformationInfo(VersionInfo, RFU, ReaderType, TrType, InventoryScanTime);
	    			if(result==0){
	    				Message message = mHandler.obtainMessage();
	    				message.what = MSG_SHOW_PROPERTIES;
	    				String str_version = String.valueOf(VersionInfo[1]);
	    				if(str_version.length()==1)str_version="0"+str_version;
	    				str_version=String.valueOf(VersionInfo[0])+"."+str_version;
	    				message.obj = str_version;
	    				mHandler.sendMessage(message);
	    			}
	    			else
	    			{
	    				UpdateErrorToast("读取版本号失败");
	    			}
	            };
	        }.start();
			
		}
		
		else if(bSetAntenna ==view)
		{
			try {
				HfData.mlist.clear();
				if((Ant1.getVisibility()==View.VISIBLE)&&(Ant1.isChecked()))
					AddAntenna(1);
				if((Ant2.getVisibility()==View.VISIBLE)&&(Ant2.isChecked()))
					AddAntenna(2);
				if((Ant3.getVisibility()==View.VISIBLE)&&(Ant3.isChecked()))
					AddAntenna(3);
				if((Ant4.getVisibility()==View.VISIBLE)&&(Ant4.isChecked()))
					AddAntenna(4);
				
			}catch (Exception e) 
			{
				Toast.makeText(
					getApplicationContext(),
					"设置失败失败",
					Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	private void AddAntenna(int Antenna)
	{
		InventoryTagMap map = new InventoryTagMap();
		map.Antenna = Antenna;
		map.isCheck =true;
		map.newlist = new ArrayList<HashMap<String, String>>();
		map.oldlist = new ArrayList<HashMap<String, String>>();
		map.autolist = new ArrayList<HashMap<String, String>>();
		HfData.mlist.add(map);
	}
	
	private void showResult(String version){
		tvVersion.setText(version.toUpperCase());
	}
	
	private String getVersion(byte[] b){
		if(b!= null && b.length == 2) return b[0]+"."+b[1];
		return "";
	}
	
	private String getStr(byte[] b){
		StringBuffer sb = new StringBuffer("");
		for(int i =0; i < b.length; i++){
			sb.append(b[i]);
		}
		return sb.toString();
	}
	
}
